import { Commodity } from './../models';
import { paraMap, paramMapByArr } from './../utils';

export default {
    create(param: object) {
        const commodity = new Commodity();
        paraMap(param, commodity);
        commodity.save();
    },
    update(param: object | any) {
        const _param: any = paramMapByArr(param, ['commodity_name', 'commodity_des', 'expect', 'is_adjust', 'commodity_pics', 'commodity_types', 'status', 'memo']);
        _param.update_at = Date.now();
        return Commodity.update({ _id: param.id, user_id: param.user_id }, _param);
    },
    remove(id: string) {
        return Commodity.deleteOne({ _id: id });
    },
    findById(id: string) {
        return Commodity.findById(id);
    },
    find(param: object | any) {
        const _para: any = paramMapByArr(param, ['user_id', 'commodity_name', 'status', 'commodity_type']);
        return Commodity.find(_para).limit(param.page_size).skip(param.page_no - 1);
    },
    findByUser(param: object | any) {
        return Commodity.find({ user_id: param.user_id }).limit(param.page_size).skip(param.page_no - 1);
    }
};